<template>
	<view class="shopcart">
		<view class="hearder" @click="manage">
			<view style="color: #fff;">管理</view>
			<view>购物车({{data.order_total_num}})</view>
			<view v-if="!showbtflag">管理</view>
			<view class="quxiao" v-if="showbtflag">取消</view>
		</view>
		<u-transition :show="show">
			<view class="cartshop">
				<view class="shopping-cart" v-if="!goods_list.length">
					<view class="shopping-cart-img">
						<image src="../../static/images/z41.png"></image>
						<view  @click="toSwitch('/pages/index/index')">去逛逛</view>
					</view>
				</view>
				<view class="shujucon">
					<view class="cartshop_every" v-for="(item,index) in goods_list" :key="item.goods_id">
						<u-transition :show="show">
							<view class="choosean">
								<image src="/static/images/z44.png" v-if="checkedId.includes(index)" class="icon" @click="checkedicons(index)"></image>
								<image src="/static/images/z43.png" v-else  class="icon" @click="checkedicons(index)"></image>
							</view>
						</u-transition>
						<view class="detail_con clearfix">
							<u-transition :show="show">
								<image :src="item.image[0].file_path" class="cont_img"></image>
							</u-transition>
							<view class="detail_right">
								<view class="box_top">
									<view class="title">{{item.goods_name}}</view>
									<view class="shop_sku" v-if="item.goods_sku.goods_attr">{{item.goods_sku.goods_attr}}</view>
								</view>
								<view class="box_bottom">
									<view class="price">￥{{item.goods_sku.goods_price}}</view>
									<view class="spxq-num">
										<text @click="reduce(item.goods_id,item.goods_sku_id,item.total_num)">-</text>
										<text class="num">{{item.total_num}}</text>
										<text @click="add(item.goods_id,item.goods_sku_id)">+</text>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</u-transition>
		<view class="bot_submit" v-if="goods_list.length!=0">
			<view class="bot_left" @click="checkedicon">
				<image v-if="checked" src="/static/images/z44.png" class="icon" ></image>
				<image src="/static/images/z43.png" v-if="!checked"  class="icon iconcolor"></image>
				<text>全选</text>
			</view>
			<view class="bot_right" v-if="!showbtflag">
				<view class="total">合计:<text>￥{{totalPrice}}</text></view>
				<view class="submit" @click="paymoney('/pages/classify/submit?type=2&id='+goods_id.toString()+'&goods_sku_id='+goods_sku_id.toString())">结算</view>
			</view>
			<view class="bot_right" v-if="showbtflag" @click="delcart">
				<view class="submit delshow">删除</view>
			</view>
		</view>
		<shopro-login-modal :showLogin="showLogin" @loginhidden="loginhidden"></shopro-login-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				del: [],
				totalPrice:0,
				totalnumber:0,
				checkedId:[],
				goods_list:[],
				data:{},
				checked:false,
				show:true,
				goods_id:[],
				goods_sku_id:[],
				showbtflag:false,
				couponPrice:0,
				showLogin:false
			}
		},
		onShow() {
			this.getinit()
		},
		methods: {
			getinit(){
				this.request('/cart/lists').then(res => {
					if(res.data.code==-500){
						this.showLogin=true
						// #ifdef APP-PLUS
						uni.hideTabBar()					
						// #endif
					}
					if(res.data.code==1){
						this.goods_list = res.data.data.goods_list
						this.data = res.data.data
					}
				})
			},
			manage(){
				this.showbtflag = !this.showbtflag
			},
			add(id,sku){
				this.request('/cart/add',{goods_num:1,goods_id:id,goods_sku_id:sku}).then(res=>{
					if(res.data.code == 1){
						this.getinit()
					}else{
						that.$tip(res.data.msg)
					}
				})
				
			},
			reduce(id,sku,num) {
				var that=this
				if(num<=1){
					this.$tip("不能再少了~")
					return
				}
				this.request('/cart/sub',{goods_id:id,goods_sku_id:sku}).then(res=>{
					if(res.data.code == 1){
						this.getinit()
					}else{
						that.$tip(res.data.msg)
					}
				})
			},
			//全选
			 checkedicon(){
				this.checked=!this.checked
				if (this.checked) {
					this.totalPrice = 0;
					this.couponPrice = 0;
					this.checkedId = [];
					for (let index = 0; index < this.goods_list.length; index++) {
						 this.checkedId.push(index);
					}
					this.del = [];
					this.goods_id = [];
					this.goods_sku_id = [];
					this.checkedId.forEach(ele => {
						this.del.push(this.goods_list[ele].goods_id)
					});
					this.goods_list.map(item => {
						this.totalPrice += item.total_num * item.goods_price
						this.couponPrice += item.total_num * item.shopping_coupon_price
						this.totalnumber += Number(item.total_num)
						this.goods_id.push(item.goods_id)
						this.goods_sku_id.push(item.goods_sku_id)
					})
				} else {
					this.checkedId = [];
					this.totalPrice = 0;
					this.couponPrice = 0;
					this.totalnumber = 0
				}
			},
			//每个商品选中
			checkedicons(index){
			   var i=this.checkedId.indexOf(index);
			   if (i !== -1) {
				   this.checkedId.splice(i, 1);
			   } else {
				   this.checkedId.push(index);
			   }
			   if (this.checkedId.length == 0) {
				   this.checked = false;
			   }
			   if (this.checkedId.length == this.goods_list.length) {
				   this.checked = true;
			   }else{
					this.checked = false;
			   }
			   this.calcprice();
			   this.del = [];
			   this.goods_id = [];
			   this.goods_sku_id = [];
			   var that=this;
			   this.checkedId.forEach(ele => {
				   this.del.push(this.goods_list[ele].goods_id)
				   this.goods_id.push(this.goods_list[ele].goods_id)
				   this.goods_sku_id.push(this.goods_list[ele].goods_sku_id)
			   });
			 },
			 calcprice() {
			   this.totalPrice = 0;
			   this.couponPrice = 0;
			   this.totalnumber = 0;
			   if (this.checkedId.length == 0) {
				   return;
			   }
			   for (let index = 0; index < this.checkedId.length; index++) {
				   let item = this.goods_list[this.checkedId[index]];
				   this.totalPrice += Number(item.total_num * item.goods_price)
				   this.couponPrice += Number(item.total_num * item.shopping_coupon_price)
				   this.totalnumber += Number(item.total_num)
			   }
			},
			delcart(){
				if(this.goods_id.length){
					this.request('/cart/delete',{goods_id:this.goods_id.toString(),goods_sku_id:this.goods_sku_id.toString()}).then(res => {
						if(res.data.code==1){
							this.$tip(res.data.msg)
							this.getinit()
							this.checked = false;
							this.checkedId = []
							this.totalPrice = 0;
						}else{
							this.$tip(res.data.msg)
						}	
					})
				}else{
					this.$tip("您还没有选择宝贝哦")
				}
			},
			loginhidden(data){
				this.showLogin=data
				// #ifdef APP-PLUS
				uni.showTabBar()					
				// #endif
			},
			paymoney(route){
				if(this.goods_id.length){
					this.checked = false;
					this.checkedId = []
					this.totalPrice = 0
					uni.navigateTo({
					    url: route
					});
				}else{
					this.$tip("您还没有选择宝贝哦")
				}
			},
			toSwitch(route){
				uni.switchTab({
				    url: route
				});
			}
		}
	}
</script>

<style scoped lang="scss">
	.shopcart{
		.hearder{
			display: flex;
			justify-content: space-between;
			align-items: center;
			position: fixed;
			z-index: 99;
			width: 100%;
			background: #fff;
			border-bottom: 2rpx solid #F6F4FA;
			padding: 90rpx 4% 30rpx;
			box-sizing: border-box;
			/* #ifdef H5 */
			padding: 20rpx 4%;
			/* #endif */
			view{
				&:nth-of-type(2){
					font-size: 36rpx;
					margin-left: 50rpx;
				}
				&:nth-of-type(3){
					width: 98rpx;
					height: 42rpx;
					font-size:26rpx;
					text-align: center;
					line-height: 42rpx;
					border-radius: 42rpx;
					border: 1rpx solid #fff;
				}
				&.quxiao{
					border: 1rpx solid $uni-text-color;
					background: #FFF4F6;
					color: $uni-text-color;
				}
			}
		}
		/* 空空的购物车 */
		.shopping-cart{
			text-align: center;
			width: 100%;
			margin-top: 200rpx;
			.shopping-cart-img{
				width: 100%;
				image{
					width: 221rpx;
					height: 230rpx;
					margin: 0 auto;
					display: block;
					padding-top: 50rpx;
				}
				view{
					width: 200rpx;
					height: 75rpx;
					line-height: 75rpx;
					text-align: center;
					margin: 50rpx auto;
					background: $uni-item-color;
					border-radius: 10rpx;
					color: #fff;
				}
			}
		} 
		.cartshop{
			padding: 170rpx 4% 200rpx;
			/* #ifdef H5 */
			padding-top: 110rpx;
			/* #endif */
			.cartshop_every{
				display: flex;
				align-items: center;
				padding: 24rpx 0;
				.choosean{
					margin-right:20rpx;
					image{
						width: 32rpx;
						height: 32rpx;
					}
				}
				.detail_con{
					width: 90%;
					min-height: 188rpx;
					box-sizing: content-box;
					margin-left: 10rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;
					.cont_img{
						float: left;
						width: 188rpx;
						height: 188rpx;
						border-radius: 10rpx;
					}
					.detail_right{
						float: left;
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						width: calc(100% - 208rpx);
						min-height: 188rpx;
						margin-left: 20rpx;
						.box_top{
							.title{
								overflow: hidden;
								text-overflow: ellipsis;
								display: -webkit-box;
								-webkit-line-clamp:2;
								/*! autoprefixer: off */
								-webkit-box-orient: vertical;
								/* autoprefixer: on */
								font-size: 28rpx;
								line-height: 34rpx;
							}
							.shop_sku{
								font-size: 22rpx;
								color: #999;
								margin-top: 20rpx;
								background-color: #eee;
								padding: 4rpx 10rpx;
								display: inline-block;
								border-radius: 4rpx;
							}
						}
						.box_bottom{
							display: flex;
							justify-content: space-between;
							align-items: flex-end;
							.price{
								font-weight: 800;
								color: $uni-text-color;
							}
							.spxq-num{
								display: flex;
								text-align: right;
								margin-top: 15rpx;
								text{
									display: inline-block;
									height: 50rpx;
									line-height: 50rpx;
									text-align: center;
								}
								text:nth-of-type(1),
								text:nth-of-type(3){
									width: 50rpx;
									border:2rpx solid #999;
									color: #999;
								}
								text:nth-of-type(2){
									color: #333;
									width: 90rpx;
									border-top: 2rpx solid #999;
									border-bottom: 2rpx solid #999;
									
								}
							}
						}
					}
				}
			}
		}
		.bot_submit{
			position: fixed;
			bottom: 98rpx;
			/* #ifdef APP-PLUS */
			bottom: 0;
			/* #endif */
			width: 100%;
			height: 98rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			background: #fff;
			box-sizing: border-box;
			z-index: 99;
			border-top: 2rpx solid #eee;
			.bot_left{
				margin-left: 3%;
				image{
					width: 32rpx;
					height: 32rpx;
				}
				text{
					font-size: 32rpx;
					margin-left: 18rpx;
				}
			}
			.bot_right{
				display: flex;
				align-items: center;
				.total{
					font-size: 32rpx;
					margin-right: 30rpx;
					text{
						font-size: 32rpx;
						color: $uni-text-color;
						margin-left: 20rpx;
					}
				}
				.submit{
					background: $uni-text-color;
					width: 180rpx;
					height: 80rpx;
					line-height: 80rpx;
					border-radius: 80rpx;
					color: #fff;
					text-align: center;
					margin:0 20rpx;
					box-sizing: border-box;
				}
				.delshow{
					background: transparent;
					border: 1rpx solid $uni-text-color;
					color: $uni-text-color;
				}
			}
		}
	}
</style>
